//
//  SISSCommentListViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2017/9/18.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSCommentListViewController.h"
#import "SISSTextView.h"
#import "SISSCommentView.h"
#import "SISSCommentMidView.h"
#import "SISSCommentShowView.h"
#import "SISSCoopCommentShowView.h"
#import "SISSNetworkingManager+HouseProgress.h"
#import "SISSNetworkingManager+PassengerProgress.h"
#import "SISSNetworkingManager+RentOutProgress.h"
#import "SISSNetworkingManager+TenantProgress.h"
#import "SISSNetworkingManager+Progress.h"
#import "SISSHouseProgressModel.h"
#import "SISSPassengerProgressModel.h"
#import "SISSRentOutProgressModel.h"
#import "SISSTenantProgressModel.h"
#import "SISSComplaintViewController.h"

#import "SISSCommentModel.h"

@interface SISSCommentListViewController ()<SISSCommentViewDelegate,SISSCoopCommentShowViewDelegate>

/* containerView */
@property (weak, nonatomic) UIView *containerView;
/* 我对他的评价 */
@property (weak, nonatomic) SISSCommentShowView *toCommentShowView;
/* 他对我的评价 */
@property (weak, nonatomic) SISSCoopCommentShowView *beCommentedView;
/// 评价/追加按钮
@property (weak, nonatomic) UIButton *confirmButton;

/* 评价中间View */
@property (weak, nonatomic) SISSCommentMidView *midView;
/* 追加中间View */
@property (weak, nonatomic) SISSTextView *additonalTextView;
/* 我对别人评价id */
@property (copy, nonatomic) NSString *toAcrId;
/* 别人对我评价id */
@property (copy, nonatomic) NSString *beAcrId;
/* 是否是追加 */
@property (assign, nonatomic) BOOL isAdditionalComment;

@end

@implementation SISSCommentListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"服务评价";
    
    [self requestCommentData];
}

#pragma mark -- SISSCommentViewDelegate 评价、追加
- (void)commentViewConfirmButtonClickAction {
    
    if (self.isAdditionalComment) { // 追加
        NSLog(@"%@",self.additonalTextView.text);
        
        [self addAdditionalComment];
    } else { // 评价
        NSLog(@" %zd \n%@",self.midView.gradeStar,self.midView.commentTextView.text);
        [self addComment];
    }
}

#pragma mark -- SISSCoopCommentShowViewDelegate 申诉
- (void)rightAppealButtonClickAction {
    SISSComplaintViewController *vc = [[SISSComplaintViewController alloc] init];
    vc.acrId = self.beAcrId;
    vc.commentType = self.commentType;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"申诉");
}


#pragma mark - 点击事件
#pragma mark - 确定按钮点击事件
- (void)confirmButtonClickAction {
    
    NSLog(@"底部按钮点击");
    
    if (self.isAdditionalComment) {
        UIView *midView = [[UIView alloc] init];
        SISSTextView *additonalTextView = [[SISSTextView alloc] init];
        additonalTextView.layer.borderWidth = 1;
        additonalTextView.layer.borderColor = [UIColor groupTableViewBackgroundColor].CGColor;
        additonalTextView.placeholder = @"请输入追加评价，不要包含特殊字符。";
        [midView addSubview:additonalTextView];
        self.additonalTextView = additonalTextView;
        [additonalTextView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(midView).offset(16);
            make.height.mas_equalTo(80);
            make.bottom.right.equalTo(midView).offset(-16);
        }];
        SISSCommentView *view = [[SISSCommentView alloc] initWithTitle:@"评价" midView:midView];
        view.delegate = self;
    } else {
        SISSCommentMidView *midView = [[SISSCommentMidView alloc] init];
        self.midView = midView;
        SISSCommentView *view = [[SISSCommentView alloc] initWithTitle:@"评价" midView:midView];
        view.delegate = self;
    }
}

#pragma mark - 数据请求
#pragma mark -- 评论数据
- (void)requestCommentData {
    
    WeakSelf
    if (self.houseProgressModel) {
        
        [kNetworkManager requestCommentSellerCommentBrokerInfoWithAhbpId:self.houseProgressModel.ahbpId success:^(NSArray<SISSCommentModel *> *commentArr) {
            
            if (commentArr.count == 0) {
                weakSelf.beCommentedView.hidden = YES;
            } else {
                weakSelf.beCommentedView.hidden = NO;
                weakSelf.beAcrId = commentArr.firstObject.ID;
                weakSelf.beCommentedView.commentModel = commentArr.firstObject;
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
        [kNetworkManager requestCommentBrokerCommentSellerInfoWithAhbpId:self.houseProgressModel.ahbpId success:^(NSArray<SISSCommentModel *> *commentArr) {
            
            if (commentArr.count == 0) {
                weakSelf.toCommentShowView.hidden = YES;
            } else {
                weakSelf.toCommentShowView.hidden = NO;
                SISSCommentModel *commentModel = commentArr.firstObject;
                weakSelf.toCommentShowView.commentModel = commentModel;
                NSString *buttonStr;
                if (commentModel.lstOfAdditional.firstObject) {
                    buttonStr = @"已评价";
                    weakSelf.confirmButton.userInteractionEnabled = NO;
                } else {
                    weakSelf.toAcrId = commentModel.ID;
                    weakSelf.isAdditionalComment = YES;
                    buttonStr = @"追加";
                }
                [self.confirmButton setTitle:buttonStr forState:UIControlStateNormal];
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
    } else if (self.passengerProgressModel) {
        
        [kNetworkManager requestCommentBuyerCommentBrokerInfoWithAdbpId:self.passengerProgressModel.adbpId success:^(NSArray<SISSCommentModel *> *commentArr) {
            if (commentArr.count == 0) {
                weakSelf.beCommentedView.hidden = YES;
            } else {
                weakSelf.beCommentedView.hidden = NO;
                weakSelf.beAcrId = commentArr.firstObject.ID;
                weakSelf.beCommentedView.commentModel = commentArr.firstObject;
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
        [kNetworkManager requestCommentBrokerCommentBuyerInfoWithAdbpId:self.passengerProgressModel.adbpId success:^(NSArray<SISSCommentModel *> *commentArr) {
            
            if (commentArr.count == 0) {
                weakSelf.toCommentShowView.hidden = YES;
            } else {
                weakSelf.toCommentShowView.hidden = NO;
                SISSCommentModel *commentModel = commentArr.firstObject;
                weakSelf.toCommentShowView.commentModel = commentModel;
                NSString *buttonStr;
                if (commentModel.lstOfAdditional.firstObject) {
                    buttonStr = @"已评价";
                    weakSelf.confirmButton.userInteractionEnabled = NO;
                } else {
                    weakSelf.toAcrId = commentModel.ID;
                    weakSelf.isAdditionalComment = YES;
                    buttonStr = @"追加";
                }
                [self.confirmButton setTitle:buttonStr forState:UIControlStateNormal];
            }
            
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
    } else if (self.rentOutProgressModel) {
        
        [kNetworkManager requestCommentLandlordCommentBrokerInfoWithAlbpId:self.rentOutProgressModel.albpId success:^(SISSCommentModel *commentModel) {
            
            weakSelf.beCommentedView.hidden = !commentModel;
            weakSelf.beAcrId = commentModel.ID;
            weakSelf.beCommentedView.commentModel = commentModel;
            
        } failed:^(NSError *error) {
            
             NSLog(@"%@",error);
        }];
        
        [kNetworkManager requestCommentBrokerCommentLandlordInfoWithAlbpId:self.rentOutProgressModel.albpId success:^(SISSCommentModel *commentModel) {
            
            weakSelf.toCommentShowView.hidden = !commentModel;
            weakSelf.toCommentShowView.commentModel = commentModel;
            NSString *buttonStr;
            if (commentModel.lstOfAdditional.firstObject) {
                buttonStr = @"已评价";
                weakSelf.confirmButton.userInteractionEnabled = NO;
            } else if (commentModel) {
                weakSelf.toAcrId = commentModel.ID;
                weakSelf.isAdditionalComment = YES;
                buttonStr = @"追加";
            }
            if (commentModel) [self.confirmButton setTitle:buttonStr forState:UIControlStateNormal];
            
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
        
    } else if (self.tenantProgressModel) {
        
        [kNetworkManager requestCommentTenantCommentBrokerInfoWithArbpId:self.tenantProgressModel.arbpId success:^(SISSCommentModel *commentModel) {
            
            weakSelf.beCommentedView.hidden = !commentModel;
            weakSelf.beAcrId = commentModel.ID;
            weakSelf.beCommentedView.commentModel = commentModel;
            
        } failed:^(NSError *error) {
           
            NSLog(@"%@",error);
        }];
        
        [kNetworkManager requestCommentBrokerCommentTenantInfoWithArbpId:self.tenantProgressModel.arbpId success:^(SISSCommentModel *commentModel) {
            
            weakSelf.toCommentShowView.hidden = !commentModel;
            weakSelf.toCommentShowView.commentModel = commentModel;
            NSString *buttonStr;
            if (commentModel.lstOfAdditional.firstObject) {
                buttonStr = @"已评价";
                weakSelf.confirmButton.userInteractionEnabled = NO;
            } else if (commentModel) {
                weakSelf.toAcrId = commentModel.ID;
                weakSelf.isAdditionalComment = YES;
                buttonStr = @"追加";
            }
            if (commentModel) [self.confirmButton setTitle:buttonStr forState:UIControlStateNormal];
            
        } failed:^(NSError *error) {
            
           NSLog(@"%@",error);
        }];
    }
}


#pragma mark -- 添加评论
- (void)addComment {
    
    WeakSelf
    NSString *content = self.midView.commentTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    if (self.houseProgressModel) {
        NSDictionary *parameters = @{
                                    @"atlrId":@"-1",
                                    @"ahbpId":self.houseProgressModel.ahbpId,
                                    @"score":@(self.midView.gradeStar),
                                    @"content":content,
                                    @"takelook_type":@(-1),
                                    @"comment_type":@(2)
                                    };
        [kNetworkManager insertBrokerCommentSellerWithParameters:parameters success:^(id responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                [SISSTools showSVWithTitle:@"评论成功"];
                [weakSelf requestCommentData];
            }
        } failed:^(NSError *error) {
            NSLog(@"%@",error);
        }];
        
    } else if (self.passengerProgressModel) {
        
        NSDictionary *parameters = @{
                                     @"atlrId":@"-1",
                                     @"adbpId":self.passengerProgressModel.adbpId,
                                     @"score":@(self.midView.gradeStar),
                                     @"content":content,
                                     @"takelook_type":@(-1),
                                     @"comment_type":@(2)
                                     };
        
        [kNetworkManager insertBrokerCommentBuyerWithParameters:parameters success:^(id responseObject) {
            
            [SISSTools showSVWithTitle:@"评论成功"];
            [weakSelf requestCommentData];
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    } else if (self.rentOutProgressModel) {
        
        NSDictionary *parameter = @{
                                    @"spId":[SISSUserAccount shared].spId,
                                    @"ltlrId":@"-1",
                                    @"alId":self.rentOutProgressModel.alId,
                                    @"albpId":self.rentOutProgressModel.albpId,
                                    @"score":@(self.midView.gradeStar),
                                    @"content":content,
                                    @"comment_type":@(2)
                                    };
        
        [kNetworkManager insertBrokerCommentLandlordWithParameters:parameter success:^(id responseObject) {
            
            [SISSTools showSVWithTitle:@"评论成功"];
            [weakSelf requestCommentData];
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
        
    } else if (self.tenantProgressModel) {
        NSDictionary *parameter = @{
                                    @"spId":[SISSUserAccount shared].spId,
                                    @"ltlrId":@"-1",
                                    @"arId":self.tenantProgressModel.arId,
                                    @"arbpId":self.tenantProgressModel.arbpId,
                                    @"score":@(self.midView.gradeStar),
                                    @"content":content,
                                    @"comment_type":@(2)
                                    };
        
        [kNetworkManager insertBrokerCommentTenantWithParameters:parameter success:^(id responseObject) {
            
            [SISSTools showSVWithTitle:@"评论成功"];
            [weakSelf requestCommentData];
        } failed:^(NSError *error) {
            
            NSLog(@"%@",error);
        }];
    }
}
#pragma mark -- 添加追加评论
- (void)addAdditionalComment {
    WeakSelf
    //    NSString *content = [NSString unicodeToISO88591:self.additonalTextView.text];
    NSString *content = self.additonalTextView.text;
    if ([NSString stringContainsEmoji:content]) {
        [self alertActionAlertWithTitle:@"提示" message:@"暂不支持表情等特殊字符评价。"];
        return;
    }
    [kNetworkManager insertAdditionalCommentWithAcrId:self.toAcrId content:content commentType:self.commentType success:^(id responseObject) {
        
        
        [SISSTools showSVWithTitle:@"评论成功"];
        [weakSelf requestCommentData];
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}


#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self.view addSubview:scrollView];
    
    // 1. 添加一个视图，四周和 scrollView 一致
    UIView *containerView = [[UIView alloc] init];
    containerView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:containerView];
    self.containerView = containerView;
    
    // 2. 自动布局
    WeakSelf
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.top.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollView);
        make.width.mas_equalTo(SCREEN_WIDTH);
        // 通过宽高设置 contentSize
        //        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 1.5));
    }];
    
    
    // 我对他的评价
    SISSCommentShowView *toCommentView = [[SISSCommentShowView alloc] init];
    toCommentView.titleStr = @"我对他的评价";
    toCommentView.hidden = YES;
    [self.containerView addSubview:toCommentView];
    self.toCommentShowView = toCommentView;
    
    // 他对我的评价
    SISSCoopCommentShowView *beCommentedView = [[SISSCoopCommentShowView alloc] init];
    beCommentedView.titleStr = @"他对我的评价";
    beCommentedView.isShowRightButton = YES;
    beCommentedView.delegate = self;
    beCommentedView.hidden = YES;
    [self.containerView addSubview:beCommentedView];
    self.beCommentedView = beCommentedView;
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"评价" forState:UIControlStateNormal];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    CGFloat buttonHeight = 44;
    WeakSelf
    [self.toCommentShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.containerView);
    }];
    
    [self.beCommentedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.containerView);
        make.top.equalTo(weakSelf.toCommentShowView.mas_bottom);
        make.bottom.equalTo(weakSelf.containerView).offset(-buttonHeight);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.mas_bottomLayoutGuideTop);
        make.height.mas_equalTo(buttonHeight);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
